package com.wangmei.service.serviceimpl;

import com.wangmei.mapper.CategeryMapper;
import com.wangmei.model.Categery;
import com.wangmei.model.CategeryExample;
import com.wangmei.service.CategeryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class CategeryServiceImpl implements CategeryService {

    @Autowired
    private CategeryMapper categeryMapper;


    @Override
    public int countByExample(CategeryExample example) {
        return categeryMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(CategeryExample example) {
        return categeryMapper.deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Integer cid) {
        return categeryMapper.deleteByPrimaryKey(cid);
    }

    @Override
    public int insert(Categery record) {
        return categeryMapper.insert(record);
    }

    @Override
    public int insertSelective(Categery record) {
        return categeryMapper.insertSelective(record);
    }

    @Override
    public List<Categery> selectByExample(CategeryExample example) {
        return categeryMapper.selectByExample(example);
    }

    @Override
    public Categery selectByPrimaryKey(Integer cid) {
        return categeryMapper.selectByPrimaryKey(cid);
    }

    @Override
    public int updateByExampleSelective(Categery record, CategeryExample example) {
        return categeryMapper.updateByExampleSelective(record,example);
    }

    @Override
    public int updateByExample(Categery record, CategeryExample example) {
        return categeryMapper.updateByExample(record,example);
    }

    @Override
    public int updateByPrimaryKeySelective(Categery record) {
        return categeryMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Categery record) {
        return categeryMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<Integer> getCidAndChildcidByCname(String cname) {
        Set<Categery> set = new HashSet<>();
        Categery categery = categeryMapper.getCategeryByCname(cname);
        /*set.add(categery);*/
        findChildCategery(set,categery.getCid());
        List<Integer> cidList = new ArrayList<>();
        for (Categery ca : set){
            cidList.add(ca.getCid());
        }
        return cidList;
    }

    @Override
    public List<String> getCnameAndParentcnameByCid(int cid) {
        Set<Categery> set = new HashSet<>();
        findParentCategery(set,cid);
        List<String> cnameList = new ArrayList<>();
        for (Categery categery : set){
            cnameList.add(categery.getCname());
        }
        return cnameList;
    }
    public Set<Categery> findParentCategery(Set<Categery> set, int cid){
        Categery categery = categeryMapper.selectByPrimaryKey(cid);
        if (categery!=null){
            set.add(categery);
        }
        if (categery!=null)
            findParentCategery(set,categery.getPid());
        return set;
    }

    //递归遍历子节点
    public Set<Categery> findChildCategery(Set<Categery> set, int cid){
        Categery categery = categeryMapper.selectByPrimaryKey(cid);
        //添加父节点
        if (categery!=null){
            set.add(categery);
        }
        List<Categery> categeryList = categeryMapper.getCategeryByPid(cid);
        for (Categery ca : categeryList){
            findChildCategery(set,ca.getCid());
        }
        return set;
    }



}
